<template>
  <div>
    <el-dialog :title="user.isAdd?'角色添加':'角色修改'" :visible.sync="user.isshow" @closed="close">
      <el-form :model="form">
        <el-form-item label="角色名称" label-width="120px">
          <el-input v-model="form.rolename" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="角色权限" label-width="120px">
          <el-tree :data="data" ref="tree" show-checkbox node-key="id" :props="defaultProps"></el-tree>
        </el-form-item>

        <el-form-item label="状态" label-width="120px">
          <el-switch v-model="form.status" :active-value="1" :inactive-value="2"></el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancle">取 消</el-button>
        <el-button type="primary" @click="add" v-if="user.isAdd">添加</el-button>
        <el-button type="primary" @click="edit" v-else>修改</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { mapGetters, mapActions } from "vuex";
import { indexRouters } from "../../../router";
import {
  reqmenuList,
  reqroleadd,
  reqroleinfo,
  reqroleedit,
} from "../../../request/api";
import { successAlert } from "../../../utils/alert";
export default {
  props: ["user", "list"],
  mounted() {
    // for (let index in indexRouters) {
    //   indexRouters[index].label = indexRouters[index].meta.title;
    //   indexRouters[index].id = Number(index);
    // }

    // 获取菜单管理中的
    reqmenuList({
      istree: true,
    }).then((res) => {
      // console.log(res);
      if (res.data.code === 200) {
        this.data = res.data.list;
        console.log(this.data);
      }
    });
  },
  computed: {
    ...mapGetters({
      info: "user",
    }),
  },
  methods: {
    ...mapActions({
      changeUser: "reqlogin",
    }),
    edit() {
      // 再次获取到当要修改的时候选中的muens  要不然这个menus一直都是你添加的时候的值
      this.form.menus = JSON.stringify(this.$refs.tree.getCheckedKeys());
      reqroleedit({
        id: this.form.id,
        rolename: this.form.rolename,
        menus: this.form.menus,
        status: this.form.status,
      }).then((res) => {
        if (res.data.code === 200) {
          // console.log(this.form);
          console.log(this.info, this.form);
          if (this.info.roleid === this.form.id) {
            this.changeUser({});
            this.$router.replace("/login");
          }
          successAlert(res.data.msg);
          this.cancle();
          this.empty();
          this.$emit("init");
        }
      });
    },
    cancle() {
      this.user.isshow = false;
    },

    close() {
      if (!this.user.isAdd) {
        this.empty();
      }
    },

    // 获取一条数据
    getOne(id) {
      reqroleinfo({
        id: id,
      }).then((res) => {
        this.form = res.data.list;
        this.form.id = id;
        console.log(this.form.menus);
        console.log(JSON.parse(this.form.menus));
        // this.form.menus = true ? this.form.menus : "[1000]";
        // 显示已选中的选项  设置目前勾选的的（选项）节点
        this.$refs.tree.setCheckedKeys(JSON.parse(this.form.menus));
      });
    },

    // 添加
    add() {
      this.user.isshow = false;

      // 获取当前被选中的选项的key值
      // let result = this.$refs.tree
      //   .getCheckedKeys()
      //   .concat(this.$refs.tree.getHalfCheckedKeys());
      // this.form.menus = "[" + result.toString() + "]";
      this.form.menus = JSON.stringify(this.$refs.tree.getCheckedKeys());
      console.log(this.form);
      // console.log(result);
      reqroleadd(this.form).then((res) => {
        if (res.data.code === 200) {
          successAlert(res.data.msg);
          this.empty();
          this.$emit("init");
        }
      });
    },

    empty() {
      // 清空  也就是传一个空数组
      this.$refs.tree.setCheckedKeys([]);
      this.form = {};
    },
  },
  props: ["user"],
  data() {
    return {
      indexRouters,
      result: [],
      input: "",
      // isshow: true,
      form: {
        rolename: "",
        menus: [],
        status: "",
      },
      data: [],
      defaultProps: {
        children: "children",
        label: "title", //显示的字段
      },
    };
  },
};
</script>

<style>
</style>